/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:assign-tracer
 * Create Time: 2016年2月19日 下午9:18:26
 */
package com.lenovo.lmrp.server.assigntracer.domain.workflow;

import java.util.Objects;

import com.lenovo.lmrp.api.model.entity.ProcessPO;
import com.lenovo.lmrp.api.model.entity.TicketPO;
import com.lenovo.sdc.framework.dao.ADaoFactory;
import com.lenovo.sdc.framework.dao.ICommonDAO;
import com.lenovo.sdc.framework.dao.ICommonDAO.FinderEnum;
import com.lenovo.sdc.framework.dao.IQueryExecutor;
import com.lenovo.sdc.framework.util.exception.ErrorCode;
import com.lenovo.sdc.framework.util.exception.TraceableException;

/**
 * 
 * @author wujin
 * @version v1.0
 */
public class ProcessSelector {
    
    public ProcessPO choiceProcess(String itsmCode, TicketPO ticketPO) {
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO()) {
            IQueryExecutor executor = dao.getRegisteredFinder(FinderEnum.HQL);
            ProcessPO process = executor.getSingleRecord(
                "from ProcessPO where itsmcode=? and mainflag=? and status = ?", 
                ProcessPO.class, 
                new Object[]{itsmCode, true, ProcessPO.ProcessStatus.PUBLISHED});
            
            if (Objects.nonNull(process))
                return process;
            else
                throw new TraceableException(ErrorCode.DB_RECORD_NOTFOUND, 
                    "not found suitable process for ticket:" + ticketPO.getId());
        }
        catch (Exception exp) {
            throw new TraceableException(ErrorCode.DB_QUERY_FAILED, exp);
        }
    }
}
